define(function () {

    var self, signaturePad, pageUi, scene_page;

    function Scene() {
        self = this;
        scene_page = app.pageParameters['scene_page'];
        //页面传过来的数据
        self.details = app.cloneJSON(scene_page.obj);
        app.extend(self.details, {
            signature: '',
            statsMoney: 0,
            MMRG011: '',
            MMRG013: '',
            MMRG008: '',
            MMRG100: '1',
            pjlist: [],
            pjsum: 0,
            expensesList: [],
            expsum: 0
        });
        self.data = {
            expenses: []
        };
        self.imgArr = [];
        self.init();
    }

    function resizeCanvas() {
        var ratio = Math.max(window.devicePixelRatio || 1, 1);
        pageUi.canvas.width = pageUi.canvas.offsetWidth * ratio;
        pageUi.canvas.height = pageUi.canvas.offsetHeight * ratio;
        pageUi.canvas.getContext("2d").scale(ratio, ratio);
        signaturePad.clear();
    }

    Scene.prototype.signature_pad = function () {
        require(["signature_pad"], function (SignaturePad) {
            signaturePad = new SignaturePad(pageUi.canvas, {
                backgroundColor: 'rgb(255, 255, 255)'
            });

            resizeCanvas();
        });
    };

    // 获取服务费列表
    Scene.prototype.getExpense = function () {
        app.ajax({
            xurl: "api/wx/mmr/getMmrcList",
            cb: function (result) {
                if (result.ok) {

                    self.data.expenses.pushArray(result.data.data);
                }
            }
        });
    };

    Scene.prototype.init = function () {

        mui.ready(function () {

            new Vue({
                el: app.getByid(doc, 'scene1'),
                data: {
                    des: self.details,
                    expenses: self.data.expenses,
                    imgs: self.imgArr
                },
                watch: {
                    'des.pjlist': {
                        handler: function (val) {
                            var c = 0;
                            val.forEach(function (v) {
                                // 是否收费
                                if (v.MMRH002 == '1') {
                                    c += (Number(v.MMRS009) * Number(v.MMRH004));
                                }
                            });
                            self.details.pjsum = c;
                            self.details.statsMoney = c + self.details.expsum;
                        },
                        deep: true
                    },
                    'des.expensesList': {
                        handler: function (val) {
                            var c = 0;
                            val.forEach(function (v) {
                                c += (Number(v.MMRI003));
                            });
                            self.details.expsum = c;
                            self.details.statsMoney = c + self.details.pjsum;
                        },
                        deep: true
                    }
                },
                methods: {
                    showSignature: function () {
                        pageUi.signatureDiv.show();
                        resizeCanvas();
                    },
                    delBj: function (i) {
                        app.confirm({
                            msg: '是否删除配件？',
                            ok: function () {
                                self.details.pjlist.removei(i);
                            }
                        });
                    },
                    // 添加配件
                    addBj: function () {
                        app.openPage({
                            src: 'wdbg/wdbj.html',
                            argsKey: 'wdbj_page',
                            args: {
                                cb: function (bjs) {
                                    for (var i = 0, j = self.details.pjlist.length; i < j; i++) {
                                        var v = self.details.pjlist[i];
                                        if (bjs.filterKV({'GWAML001': v.GWAML001}).length > 0) {
                                            return v.GWAML003;
                                        }
                                        v.MMRH001 = '1';// 处理方式
                                    }
                                    self.details.pjlist.pushArray(bjs);
                                    return 1;
                                }
                            }
                        });
                    },
                    addExpenses: function () {
                        var item = self.data.expenses[0];
                        self.details.expensesList.push({
                            MMRI001: item.MMRC001,
                            MMRI002: item.MMRC002,
                            MMRI003: item.MMRC002
                        });
                    },
                    expensesChange: function (v) {
                        var exp = self.data.expenses.filterMaps([{'MMRC001': v.MMRI001}])[0];
                        v.MMRI002 = v.MMRI003 = exp.MMRC002;
                    },
                    delExpenses: function (i) {
                        self.details.expensesList.removei(i);
                    },
                    orderSumit: function () {
                        if (self.details.MMRG008 == '') {
                            mui.toast('需要现场签到');
                            return;
                        }
                        if (self.details.MMRG011 == '') {
                            mui.toast('需要填写客户意见');
                            return;
                        }
                        if (self.details.signature == '') {
                            mui.toast('需要客户签字');
                            return;
                        }
                        app.showLoading();
                        app.uploadLocalIds({
                            ids: self.imgArr,
                            cb: function (imgs) {
                                var imgurl = "";
                                for (var i = 0; i < imgs.length; i++) {
                                    imgurl += (imgs[i].name + ",");
                                }
                                self.details.MMRG009=imgurl.substring(0,imgurl.length-1);
                                //self.details.MMRG009 = imgs.join(',');
                                app.ajax({
                                    xurl: "api/wx/xchb/xchbSave",
                                    xdata: {obj: app.jsonString(self.details)},
                                    cb: function (result) {
                                        app.hideLoading();
                                        if (result.ok) {
                                            app.alert({
                                                msg: '回报单提交成功!',
                                                ok: function () {
                                                    mui.back();
                                                }
                                            });
                                            scene_page.cb();
                                        }
                                    }
                                });
                            }
                        });

                    },
                    delimg: function (i) {
                        self.imgArr.removei(i);
                    },
                    addImgs: function () {
                        app.chooseImageNew(function (imgs) {
                            //self.imgArr.pushArray(imgs);
                            for (var i = 0; i < imgs.length; i++) {
                                self.imgArr.push({
                                    img: imgs[i].img,
                                    id: imgs[i].localId
                                })
                            }
                        });
                    }
                }
            });

            pageUi = {
                signatureDiv: $('.mui-backdrop'),
                canvas: doc.querySelector("canvas"),
                signatureImg: doc.getElementById('signatureImg'),
                scroll: mui('.mui-scroll-wrapper').scroll({
                    scrollY: true,
                    scrollX: false,
                    deceleration: 0.0005,
                    bounce: false
                }),
                mask: mui.createMask(function () {
                    $('.modal1').hide();
                    pageUi.signInSuccessDiv.hide();
                }),
                nAddress: doc.getElementById('nAddress'),
                signInSuccessDiv: $('.modal2')
            };

            app.taps.call(win, [
                {
                    o: 'clearSignature',
                    cb: function (e) {
                        signaturePad.clear();
                    }
                }, {
                    o: 'signature',
                    cb: function (e) {
                        self.details.signature = signaturePad.toDataURL();
                        pageUi.signatureDiv.hide();
                    }
                }, {
                    o: 'backdrop',
                    cb: function (e) {
                        signaturePad.clear();
                        pageUi.signatureDiv.hide();
                    }
                }, {
                    o: 'signature-pad--body',
                    cb: function (e) {

                        return false;
                    }
                }, {
                    o: 'btn2',
                    cb: function (e) {
                        $('.sceneCont').show();
                        pageUi.scroll.reLayout();
                        pageUi.scroll.scrollTo(0, -$('.sceneCont')[0].offsetTop, 400);
                    }
                }, {
                    o: 'btnSignIn_cancel',
                    cb: function (e) {
                        pageUi.mask.close();
                        $('.modal1').hide();
                    }
                }, {
                    o: 'btnSignIn_sure',
                    cb: function (e) {

                        app.regeoByLocation({
                            cb: function (res) {
                                pageUi.nAddress.innerHTML = res.formatted_address;
                                $('.modal1').hide();
                                pageUi.signInSuccessDiv.show();
                                self.details.MMRG008 = res.formatted_address;
                                self.details.MMRG007 = (new Date()).Format('yyyy-MM-dd hh:mm:ss');
                            },
                            //lat: 30.2361,
                            //lng: 120.18663,
                            loading: true
                        });
                    }
                }, {
                    o: 'btnSignIn',
                    cb: function (e) {
                        pageUi.mask.show();
                        $('.modal1').show();
                    }
                }, {
                    o: 'btnSignIn_succ',
                    cb: function (e) {
                        //document.getElementById('sign_time').value = self.formt(new Date());
                        pageUi.mask.close();
                    }
                }
            ]);
        });

        self.signature_pad();
        self.getExpense();

    };

    return new Scene();

});